package com.ty.zls.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.ty.zls.R;
import com.ty.zls.adapter.ArreageAdapter;
import com.ty.zls.adapter.CheckWaterAdapter;
import com.ty.zls.components.MyReceycleView;
import com.ty.zls.listener.ArreageItemListener;
import com.ty.zls.listener.ArreageLoadMoreListener;
import com.ty.zls.listener.ArreageRefreshListener;
import com.ty.zls.listener.WaterCheckLoadMoreListener;
import com.ty.zls.listener.WaterCheckRefreshListener;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

public class CheckWaterActivity extends AppCompatActivity implements View.OnClickListener {

    @ViewInject(R.id.checkwater_topbar)
    private QMUITopBarLayout checkwater_topbar;
    @ViewInject(R.id.rb_check_water)
    private QMUIRoundButton search;
    @ViewInject(R.id.et_checkwater_search_content)
    private EditText searchContent;
    @ViewInject(R.id.swipe_check_water)
    private SwipeToLoadLayout loadLayout;
    @ViewInject(R.id.swipe_target)
    private MyReceycleView swipeTarget;
    @ViewInject(R.id.ev_check_water)
    private QMUIEmptyView emptyView;
    WaterCheckRefreshListener refreshListener;
    WaterCheckLoadMoreListener waterCheckLoadMoreListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        QMUIStatusBarHelper.translucent(this);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_water);
        x.view().inject(this);
        initTopBar();
        initCheckWaterDatas();
        search.setOnClickListener(this);
    }


    private void initTopBar() {
        checkwater_topbar.setTitle("抄表复核");
        checkwater_topbar.setBackgroundColor(getResources().getColor(R.color.app_color_theme_7));
        checkwater_topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckWaterActivity.this.finish();
            }
        });
    }


    private void initCheckWaterDatas(){
        swipeTarget.setEmptyView(emptyView);
        swipeTarget.setLayout(loadLayout);
        swipeTarget.setLayoutManager(new LinearLayoutManager(this));
        final CheckWaterAdapter adapter = new CheckWaterAdapter(this);
        swipeTarget.setAdapter(adapter);

        refreshListener = new WaterCheckRefreshListener(adapter, this, loadLayout, emptyView);
        loadLayout.setOnRefreshListener(refreshListener);
        waterCheckLoadMoreListener = new WaterCheckLoadMoreListener(adapter, this, loadLayout, emptyView);
        loadLayout.setOnLoadMoreListener(waterCheckLoadMoreListener);
        adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                checkwater_topbar.setTitle("抄表复核("+adapter.getDatas().size()+")");
            }
        });
        refreshListener.autoRefresh();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.rb_check_water) {
            refreshListener.setSearch(searchContent.getText().toString());
            waterCheckLoadMoreListener.setSearch(searchContent.getText().toString());
            refreshListener.autoRefresh();
        }
    }
}
